#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
using namespace std;

const int kMaxN = 1e4 + 10;
const int kMaxM = 1e4 + 10;
const int kMaxK = 1e4 + 10;
const int kInf = 1e9;

vector<pair<int, int>> g[kMaxK + kMaxM + kMaxN];
int dis[kMaxK + kMaxM + kMaxN];

int ToHash(int r, int c, int m) {
  return r * (m + 2) + c;
}

void AddEdge(int u, int v, int cuv, int cvu) {
  g[u].emplace_back(v, cuv);
  g[v].emplace_back(u, cvu);
}

int BuildNewGraph(int &b, int &e) {
  int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  unordered_map<int, int> ids;
  int n, m, k;

  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    int r, c;

    cin >> r >> c;
    ids[ToHash(r, c, m)] = i;
    for (int d = 0; d < 4; d++) {
      if (ids.count(ToHash(r + dir[d][0], c + dir[d][1], m))) {
        int v = ids[ToHash(r + dir[d][0], c + dir[d][1], m)];

        AddEdge(i, v, 0, 0);
      }
    }

    if (r > 1) {  // light row r-1
      AddEdge(i, k + r - 2, 1, 0);
    }
    if (r < n) {  // light row r+1
      AddEdge(i, k + r, 1, 0);
    }
    if (c > 1) {  // light column c-1
      AddEdge(i, k + n + c - 2, 1, 0);
    }
    if (c < m) {  // light column c+1
      AddEdge(i, k + n + c, 1, 0);
    }

    AddEdge(i, k + r - 1, 1, 0);      // light row r
    AddEdge(i, k + n + c - 1, 1, 0);  // light column c
  }

  int ret = n + m + k;
  if (ids.count(ToHash(n, m, m)) == 0) {
    ids[ToHash(n, m, m)] = ret;
    AddEdge(k + n - 1, ret, 0, 1);
    AddEdge(k + n + m - 1, ret, 0, 1);
    ret++;
  }
  b = 0;
  e = ids[ToHash(n, m, m)];
  return ret;
}

int BFS(int N, int b, int e) {
  queue<int> que[2];

  fill(dis, dis + N, kInf);
  dis[b] = 0;
  que[dis[b] & 1].emplace(b);
  for (int d = 0; que[0].size() + que[1].size(); d++) {
    queue<int> &now = que[d & 1], &next = que[(d + 1) & 1];

    for (; !now.empty(); now.pop()) {
      int u = now.front();

      if (dis[u] != d) {
        continue;
      }
      for (pair<int, int> edge : g[u]) {
        int v = edge.first, vd = edge.second + d;

        if (dis[v] > vd) {
          dis[v] = vd;
          que[dis[v] & 1].emplace(v);
        }
      }
    }
  }

  return dis[e] == kInf ? -1 : dis[e];
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int b, e, N;

  N = BuildNewGraph(b, e);
  cout << BFS(N, b, e) << "\n";
  return 0;
}